<include file="public@storage"/>
</head>
<link href="__STATIC__/storage/handsontable/handsontable.full.css" rel="stylesheet">
<body>
<div class="container body-content" style="width: 100%;margin-top: 2%">
    <div class="panel panel-primary" style="margin-bottom: 0.5rem">
        <div class="panel-heading">
            <button id = "return" style="background-color:rgb(51, 122, 183);border: none ;" onclick="history.go(-1)"><span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span></button>
            <label class="title">出库单导入</label>
        </div>
        <div class="center-table" style="height: 100%;">
            <button type="button" id="type" value="0" class="mui-btn mui-btn--raised mui-btn--primary"
                    style="position: relative;left:80%;top:1%"><span id="typeText">模式1</span></button>
            <button type="button" id="ask" class="mui-btn mui-btn--raised mui-btn--primary"
                    style="position: relative;left:80%;top:1%">确认导入
            </button>
            <div id="hot" style="width:fit-content;width:-moz-fit-content;width:-webket-fit-content;margin:auto;"></div>
        </div>
    </div>
</div>
<script src="__STATIC__/storage/handsontable/handsontable.full.js"></script>
<script>
    var model = parseInt($('#type').val());
    var initData_1 = [
        ["订单号", "生产类型(必填)", "生产车间(必填)", "委外厂商(必填)", '生成仓库(必填)', "备注", '模式1'],
        ["", "", "", "", "", "", '1'],
        ["物料代码(必填)", "数量(必填)", "行号"]
    ];
    var initData_2 = [
        ["订单号", "生成仓库(必填)", "备注", '模式2'],
        ["", "", "", '2'],
        ["条码(必填)", "数量(选填)"]
    ];
    var hot;
    $(function () {
        var hotSettings = {
            data: initData_1,//初始化时候的数据
            minSpareRows: 1,//空出多少行
            minRows: 30,
            rowHeaders: true,
            autoRowSize: true,//
            columnSorting: true,//允许排序
            rowHeights: 20,
            colWidths: 200
        };
        var hotElement = document.getElementById('hot');
        hot = new Handsontable(hotElement, hotSettings);
        tableHeader();
    });

    $("#ask").click(function () {
        var row = hot.countRows();
        var data = getBomTable();
        if(data[1] === '' || data[1] == null){
            return;
        }
        console.log(data);
        $.ajax({
            url: "{:url('/storage/Bom/readManual')}",
            dataType: "json",
            type: 'POST',
            data: {
                data: data
            },
            success: function (res) {
                if (res.success === true) {
                    layer.msg(res.message);
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                } else if (res.success === false) {
                    layer.msg(res.message);
                }
            }, error: function () {
                layer.msg('网络错误');
            }
        })
    });

    var typeClick = $('#type').click(
        function () {
            var typeText = ['模式1', '模式2'];
            var type = $('#type').val();
            var selectValue = type == 1 ? 0 : 1;
            $('#typeText').text(typeText[selectValue]);
            $('#type').val(selectValue);
            model = selectValue;
            var data = model === 1 ? initData_2 : initData_1;
            hotSettings = {
                data: data,//初始化时候的数据
                minSpareRows: 1,//空出多少行
                minRows: 20,
                rowHeaders: true,
                autoRowSize: true,//
                columnSorting: true,//允许排序
                rowHeights: 20,
                colWidths: 200
            };
            hot.destroy();
            hotElement = document.getElementById('hot');
            hot = new Handsontable(hotElement, hotSettings);
            tableHeader();
        }
    );

    tableHeader = function () {
        hot.updateSettings({
            cells: function (row, col, prop) {
                var cellProperties = {};
                if (row === 0) {
                    cellProperties.readOnly = true
                }
                if (row === 2) {
                    cellProperties.readOnly = true
                }
                if (row === 1 && col === 6 && model === 0) {
                    cellProperties.readOnly = true
                }
                if (row === 1 && col === 3 && model === 1) {
                    cellProperties.readOnly = true
                }
                return cellProperties;
            }
        });
    };

    getBomTable = function () {
        var tableData = '';
        switch (model) {
            case 0:
                tableData =  getTableData_Model_1();
                break;
            case 1:
                tableData = getTableData_Model_2();
                break;
        }
        return tableData;
    };

    getTableData_Model_1 = function () {
        var row = hot.countRows();
        var sourceData = hot.getData();
        var tableData = [];
        for (var i = 0; i < row; i++) {
            tableData[i] = new Array(7);
        }

        if (sourceData[1][1] == null || sourceData[1][1] === '') {
            layer.msg('请填写生产类型!');
            return false;
        }
        if (sourceData[1][2] == null || sourceData[1][2] === '') {
            layer.msg('请填写生产车间!');
            return false;
        }
        if (sourceData[1][3] === null || sourceData[1][3] === '') {
            layer.msg('请填写委外厂商!');
            return false;
        }
        if (sourceData[1][4] === null || sourceData[1][4] === '') {
            layer.msg('请填写生成仓库!');
            return false;
        }

        tableData[0][0] = sourceData[1][0]; //单号
        tableData[0][1] = sourceData[1][1]; //生产类型
        tableData[0][2] = sourceData[1][2]; //生产车间
        tableData[0][3] = sourceData[1][3]; //委外厂商
        tableData[0][4] = sourceData[1][4]; //生成仓库
        tableData[0][5] = sourceData[1][5]; //备注
        tableData[0][6] = sourceData[1][6]; //模式
        var k = 1;
        for (var j = 3; j < row; i++) {
            var count = 0;
            if (sourceData[j][0] !== null && sourceData[j][0] !== '') {
                count++;
            }
            if (sourceData[j][1] !== null && sourceData[j][1] !== '') {
                count++;
            }
            if (count === 2) {
                tableData[k][0] = sourceData[j][0];
                tableData[k][1] = sourceData[j][1];
                tableData[k++][2] = sourceData[j][2];
            } else if (count === 1) {
                layer.msg('第' + (j + 1) + '行数据不完整');
                return false;
            }
        }
        return tableData;
    };

    getTableData_Model_2 = function () {
        var row = hot.countRows();
        var sourceData = hot.getData();
        var tableData = [];
        for (var i = 0; i < row; i++) {
            tableData[i] = new Array(4);
        }
        if (sourceData[1][1] == null || sourceData[1][1] === '') {
            layer.msg('请填写生成仓库!');
            return;
        }

        if (sourceData[1][0] != null && sourceData[1][0] !== '') {
            tableData[0][0] = sourceData[1][0]; //单号
        }
        if (sourceData[1][1] !== null && sourceData[1][1] !== '') {
            tableData[0][1] = sourceData[1][1]; //仓库
        }
        if (sourceData[1][2] !== null && sourceData[1][2] !== '') {
            tableData[0][2] = sourceData[1][2]; //备注
        }
        if(sourceData[1][3] !== null && sourceData[1][3] !== ''){
            tableData[0][3] = sourceData[1][3]; //模式
        }

        var k = 0;
        for (var  j = 3; j < row; j++) {
            var col_1 = '';
            var col_2 = '';
            col_1 = sourceData[j][0];
            col_2 = sourceData[j][1];
            if ((col_1 == null || col_1 === '') && (col_2 !== ''&& col_2 !== null)) {
                layer.msg('第' + (j + 1) + '行数据不完整');
                return;
            }
            if(sourceData[j][0] != null && sourceData[j][0] !== '') {
                tableData[k][0] = sourceData[j][0];
            }
            if(sourceData[j][1] != null && sourceData[j][1] !== ''){
                tableData[k][1] = sourceData[j][1]
            }
            k++;
        }
        return tableData;
    };
</script>
</body>
